<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form class="-mb-15px" :model="queryParams" ref="queryFormRef" :inline="true">
      <el-form-item label="名称(型号)" prop="model">
        <el-input
          v-model="queryParams.model"
          placeholder="请输入型号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <!--      <el-form-item label="流量计编号" prop="serialNumber">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.serialNumber"-->
      <!--          placeholder="请输入流量计编号"-->
      <!--          clearable-->
      <!--          @keyup.enter="handleQuery"-->

      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="流量最小值" prop="minFlow">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.minFlow"-->
      <!--          placeholder="请输入流量最小值"-->
      <!--          clearable-->
      <!--          @keyup.enter="handleQuery"-->

      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="流量最大值" prop="maxFlow">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.maxFlow"-->
      <!--          placeholder="请输入流量最大值"-->
      <!--          clearable-->
      <!--          @keyup.enter="handleQuery"-->

      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['system:flowmeter:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['system:flowmeter:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table
      v-loading="loading"
      :data="list"
      :stripe="true"
      :show-overflow-tooltip="true"
      ref="flowmeterTableRef"
      @selection-change="handleSelectionChange"
    >
      <!-- 新增：选择列，只在选择模式下显示 -->
      <el-table-column v-if="showSelection" type="selection" width="55" />
      <!--      <el-table-column label="id" align="center" prop="id" />-->
      <el-table-column label="名称(型号)" align="center" prop="model" />
      <el-table-column label="流量计编号" align="center" prop="serialNumber" />
      <el-table-column label="流量最小值(ml/min)" align="center" prop="minFlow" />
      <el-table-column label="流量最大值(ml/min)" align="center" prop="maxFlow" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['system:flowmeter:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['system:flowmeter:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <!-- 在选择模式下隐藏表单弹窗的引用 -->
  <FlowmeterForm v-if="!showSelection" ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { FlowmeterApi, FlowmeterVO } from '@/api/system/flowmeter'
import FlowmeterForm from './FlowmeterForm.vue'
import { ElTable } from 'element-plus' // 引入 ElTable 类型

/** 流量计 列表 */
defineOptions({ name: 'Flowmeter' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

// 新增：定义 props
const props = defineProps({
  showSelection: {
    type: Boolean,
    default: false // 默认不显示选择模式
  }
})

// 新增：定义 emits
const emit = defineEmits(['selection-change'])

const loading = ref(true) // 列表的加载中
const list = ref<FlowmeterVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  name: undefined,
  model: undefined,
  serialNumber: undefined,
  minFlow: undefined,
  maxFlow: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中
const flowmeterTableRef = ref<InstanceType<typeof ElTable>>() // 新增：el-table 的引用

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await FlowmeterApi.getFlowmeterPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
// 当在选择模式下，不开放添加/修改功能
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await FlowmeterApi.deleteFlowmeter(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await FlowmeterApi.exportFlowmeter(queryParams)
    download.excel(data, '流量计.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

// 新增：处理表格选择变化
const handleSelectionChange = (selection: FlowmeterVO[]) => {
  // 触发 selection-change 事件，将选中的数据传递给父组件
  emit('selection-change', selection)
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
